package net.documentshare.docu;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import net.documentshare.utils.IOUtils;
import net.documentshare.utils.UnFileUtils;
import net.documentshare.utils.doc.ConvertParam;
import net.documentshare.utils.doc.DocViewerConverter;
import net.documentshare.utils.doc.FileUtils;
import net.documentshare.utils.doc.PdfToImageUtil;
import net.simpleframework.util.StringUtils;

public final class DocuUtils {

	/**
	 * 转换文件
	 */
	public static void convertFile(final String filePath, final String fileName, final String docuFunction, final String id, final double allowReads,
			final int convertPages, final Map<String, String> json) {
		try {
			// 文件上传后,根据类型判断，是swf转换还是解压缩
			final String extension = StringUtils.getFilenameExtension(fileName);
			if ("docu".equals(docuFunction)) {
				convertToOnline(filePath, fileName, id, allowReads, convertPages, json);
			} else if ("RAR".equalsIgnoreCase(extension)) {
				UnFileUtils.unRar(filePath + id, filePath + fileName);
				json.put("success", "2");
			} else if ("ZIP".equalsIgnoreCase(extension)) {
				UnFileUtils.unZip(filePath + id, filePath + fileName);
				json.put("success", "2");
			}
		} catch (Exception e) {
		} finally {
		}
	}

	/**
	 * 在线文件处理
	 * 
	 * @param filePath
	 * @param docuBean
	 */
	private static void convertToOnline(final String filePath, final String fileName, final String id, final double allowRead,
			final int convertPages, final Map<String, String> json) throws Exception {
		File source = new File(filePath + File.separator + fileName);
		File dir = new File(filePath + File.separator + id);
		if (!dir.exists()) {
			dir.mkdir();
		}
		File in = source;
		boolean isSpace = false;// 默认文件名不带空格
		if (source.getName().indexOf(" ") != -1) {
			in = new File(source.getName().replace(" ", ""));
			try {
				IOUtils.copyFile(source, in);// 如果文件名中存在空格会导致后处理有问题，所以要将空格去掉创建一个临时的
			} catch (IOException e) {
				e.printStackTrace();
			}
			isSpace = true;
		}
		File finalPdf = null;
		try {
			String outPath = dir.getAbsolutePath();
			final File pdf = DocViewerConverter.toPDF(in, outPath);// 转化成pdf
			convertToSwf(pdf, outPath, allowRead, convertPages, json);// pdf转swf
			finalPdf = new File(outPath + File.separator + FileUtils.getFilePrefix(fileName) + "_decrypted.pdf");// 如果pdf加密则会生成一个_decrypted.pdf后缀的pdf
			if (!finalPdf.exists()) {
				pdfByFirstPageToJpeg(pdf, outPath);// 提取pdf首页
			} else {
				pdfByFirstPageToJpeg(finalPdf, outPath);// 提取pdf首页
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (isSpace) {
				IOUtils.delete(in);
			}
			if (finalPdf != null) {
				IOUtils.delete(finalPdf);
			}
		}
	}

	/**
	 * 提取文档首页作为缩略图
	 * 
	 * @param filePath
	 * @param docuBean
	 */
	private static void pdfByFirstPageToJpeg(final File pdf, String outPath) {
		if (pdf == null) {
			return;
		}
		PdfToImageUtil.pdfToImage(pdf, outPath, 0);
	}

	/**
	 * 把上传的文档转化成swf格式
	 * 
	 * @param filePath
	 * @param docuBean
	 */
	private static void convertToSwf(final File in, String outPath, final double allowRead, final int convertPages, final Map<String, String> json) {
		boolean split = true;
		// 有可能是百分比
//		if (allowRead * 100 > 0) {
//			split = true;
//		}
		try {
			ConvertParam param = new ConvertParam();
			param.pdfFile = in;
			param.outPath = outPath;
			param.splitPage = split;
//			if (allowRead > 0)
				param.convertPages = String.valueOf(convertPages);
			final int pages = DocViewerConverter.toSwf(param);
			if (pages != 0) {
				json.put("pages", String.valueOf(pages));
				json.put("success", "2");
			}
		} catch (Exception e) {
			json.put("success", "3");
		}
	}

}
